package com.tc.activiti.util;

import com.tc.base.exception.BaseException;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zengqingfa
 * @date 2019/10/22 15:05
 * @description
 * @email zengqingfa_java@163.com
 */
public class StringUtil extends StringUtils {
    private StringUtil() {
    }

    public static boolean isExistIgnoreCase(String var0, String var1, String var2) {
        var0 = var0.toLowerCase();
        var1 = var1.toLowerCase();
        var2 = var2.toLowerCase();
        int var4 = var0.indexOf(var1);
        int var3 = var0.indexOf(var2);
        return var4 != -1 && var3 != -1 && var4 < var3;
    }

    public static String trimPrefix(String var0, String var1) {
        for (var0 = var0; var0.startsWith(var1); var0 = var0.substring(var1.length())) {
        }

        return var0;
    }

    public static String trimSuffix(String var0, String var1) {
        for (var0 = var0; var0.endsWith(var1); var0 = var0.substring(0, var0.length() - var1.length())) {
        }

        return var0;
    }

    public static String trimBoth(String var0, String var1) {
        return trimSuffix(trimPrefix(var0, var1), var1);
    }

    public static String upperFirst(String var0) {
        return convertFirst(var0, true);
    }

    public static String lowerFirst(String var0) {
        return convertFirst(var0, false);
    }

    public static String removeSpecialChars(String var0) {
        return Pattern.compile("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\"]").matcher(var0).replaceAll("").trim();
    }

    public static String convertFirst(String var0, boolean var1) {
        if (isEmpty(var0)) {
            return "";
        } else {
            char var2 = var0.charAt(0);
            String var3 = new String(new char[]{var2});
            var3 = var1 ? var3.toUpperCase() : var3.toLowerCase();
            return var3 + var0.substring(1);
        }
    }

    public static String convertSplit(String var0, String var1) {
        if (isEmpty(var0)) {
            return "";
        } else if (isEmpty(var1)) {
            return "_";
        } else {
            String[] var3;
            if ((var3 = var0.toLowerCase().split(var1)).length == 1) {
                return var3[0];
            } else {
                int var4 = 1;

                for (int var2 = var3.length; var4 < var2; ++var4) {
                    var3[var4] = upperFirst(var3[var4]);
                }

                return join(var3, "");
            }
        }
    }

    public static String replaceVariable(String var0, Map<String, String> var1) {
        return replaceVariable(var0, var1, false);
    }

    public static String replaceVariable(String var0, Map<String, String> var1, Boolean var2) {
        Matcher var3 = Pattern.compile("\\{(.*?)\\}").matcher(var0);

        while (var3.find()) {
            String var4 = var3.group(1);
            String var5 = var3.group(0);
            String var6;
            if ((var6 = (String) var1.get(var4)) != null) {
                var0 = var0.replace(var5, var6);
            } else if (var2) {
                throw new BaseException("没有找到[" + var4 + "]对应的变量值，请检查表变量配置!");
            }
        }

        return var0;
    }

    public static String replaceVariable(String var0, String var1) {
        Matcher var2;
        if ((var2 = Pattern.compile("\\{(.*?)\\}").matcher(var0)).find()) {
            String var3 = var2.group(0);
            var0 = var0.replace(var3, var1);
        }

        return var0;
    }

    public static String trimSufffix(String var0, String var1) {
        while (var0.endsWith(var1)) {
            var0 = var0.substring(0, var0.length() - var1.length());
        }

        return var0;
    }

    public static String trim(String var0, String var1) {
        return trimSufffix(trimPrefix(var0, var1), var1);
    }

    public static String combilePath(String var0, String var1) {
        var0 = trimSufffix(var0, File.separator);
        var1 = trimPrefix(var1, File.separator);
        return var0 + File.separator + var1;
    }

    public static String pathJoin(String... var0) {
        StringBuilder var1 = new StringBuilder();
        int var2 = 0;

        for (int var3 = var0 == null ? 0 : var0.length; var2 < var3; ++var2) {
            String var4;
            if ((var4 = var0[var2]) == null) {
                var4 = "";
            }

            if (var2 > 0 && (var4.startsWith("/") || var4.startsWith("\\"))) {
                var4 = var4.substring(1);
            }

            var1.append(var4);
            if (var2 + 1 < var3 && !var4.endsWith("/") && !var4.endsWith("\\")) {
                var1.append("/");
            }
        }

        return var1.toString();
    }

    public static Boolean arrayStringContains(String var0, String var1) {
        return !isEmpty(var0) && !isEmpty(var1) ? Arrays.asList(var0.split(",")).contains(var1) : false;
    }
}
